<template>
  <div>
    <div class="box1">
      <!-- 搜索 -->
      <div class="searchcon">
        <div class="button" @click="$router.go(-1)">
          <span class="iconfont icon-fanhui"></span>
        </div>
        <div class="inputdiv">
          <input type="text" placeholder="搜索歌曲、歌手" v-model="text" />
        </div>
        <div v-show="flas" class="delbtn" @click="delbtn">X</div>
        <!-- <van-search v-model="value" placeholder="请输入搜索关键词" /> -->
      </div>
      <ul v-if="listsDefault" class="searchul">
        <li
          v-show="flas"
          v-for="item in listsDefault"
          :key="item.id"
          @click="setId(item.id,item.al.name)"
        >
          <span class="right iconfont icon-sousuo"></span><span class="span">{{ item.al.name }}</span>
          <p>{{ item.ar[0].name }}</p>
        </li>
      </ul>
    </div>
    <!-- 热门提示 -->
    <div class="popular-search">
      <p>热门搜索</p>
      <ul>
        <li @click="setdata(item)" v-for="(item, index) in items" :key="index">
          {{ item.first }}
        </li>
      </ul>
    </div>
    <!-- 搜索历史 -->
    <div class="popular-search history">
      <p>搜索历史</p>
      <div class="btn iconfont icon-shanchu" @click="delAll(lists.length)"></div>
    </div>
    <div class="listscon">
      <ul>
        <li v-for="(item, index) in lists" :key="index">
          <div @click="add(item)"><span>{{ item.text }}</span></div>
          <p @click="del(index)">{{item.del}}</p>
        </li>
      </ul>
    </div>
  </div>
</template>

<script>
import { getSearchHot } from "../api/search";
import { getSearchDefault } from "../api/search";
export default {
  data() {
    return {
      items: null,
      text: "",
      flas: false,
      playlist: null,
      lists: [],
      listsDefault: [],
    };
  },

  methods: {
    addTodo() {
      //历史记录
      //   if (this.text != "") {
      //   this.lists.push({
      //     text: this.text,
      //   });
      // }
      // this.text = ""
    },
    setId(id,name) {
      // 播放全部歌曲
      // this.$emit("get-play-all-id", this.$route.query.id); //专递歌单id
      this.$emit("get-player-id", id);
      this.flas = false;
      this.lists.push({
        text:name,
        del:"X"
      })
      this.text = ""
      
    },
    del(index) {
      //删除历史记录
      this.lists.splice(index, 1);
    },
    delAll(index) {
      //删除历史记录(全部)
      this.lists.splice(0, index);
    },
    delbtn() {
      this.text = "";
      this.flas = false;
    },
    // 热门推荐
    getSearchHotFun() {
      getSearchHot().then((data) => {
        this.items = data.result.hots;
        console.log(data);
      });
    },
    setdata(item){
      this.text = item.first
    },
    add(item){
       this.text = item.text
    }
  },
  watch: {
    text() {
      if (this.text == "") {
        this.flas = false;
      } else {
        this.flas = true;

        getSearchDefault({ keywords: this.text }).then((data) => {
          this.listsDefault = data.result.songs;
          console.log(this.listsDefault);
        });
      }
    },
  },
  created() {
    // getSearchDefault({value:this.text}).then((data) => {
    //   console.log(data);
    //   this.playlist = data.result.songs;
    // });
    this.getSearchHotFun();
  },
  components: {},
};
</script>

<style lang="less">
.box1 {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  position: relative;
  .searchcon {
    display: flex;
    width: 100%;
    height: 45px;
    background-color: #d44439;
    .button {
      width: 45px;
      height: 45px;
      line-height: 45px;
      text-align: center;
      font-size: 16px;
      color: white;
      span {
        font-weight: bolder;
        font-size: 20px;
      }
    }
    .inputdiv {
      height: 45px;
      text-align: center;
      line-height: 20px;
      display: flex;
      align-items: center;
      input {
        width: 285px;
        background-color: #d44439;
        font-size: 14px;
        border: transparent;
        color: #e8d0ce;

        border-bottom: 1px solid #e8d0ce;
      }
      ::-webkit-input-placeholder {
        color: #e8d0ce;
      }
    }
    .delbtn {
      width: 45px;
      height: 45px;
      line-height: 45px;
      color: white;
      text-align: center;
    }
    // .van-search{
    //    background-color: #D44439;
    // }
    // .van-search__content{
    //     background-color: #D44439;
    //     border-radius: 20px;
    // }
  }
  .searchul {
    position: absolute;
    top: 45px;
    left: 0px;
    width: 375px;
    z-index: 10;
    background-color: white;
    li {
      height: 40px;
      background-color: white;
      margin: 13px 0;
      color: #5f5b5f;
      overflow: hidden;
      span {
       
        padding: 0px 15px;
      }
      .span{
        padding: 0px;
         height: 24px;
        display: inline-block;
      }
      p {
        font-size: 14px;
        padding-left: 47px;
        color: #a6a6a7;
      }
    }
  }
}
.popular-search {
  padding: 7px;
  display: flex;
  flex-direction: column;
  p {
    position: relative;
    font-size: 14px;
    color: #a6a6a7;
    left: 0;
  }
  ul {
    display: flex;
    flex-wrap: wrap;
    margin-top: 5px;
    li {
      margin: 7px;
      padding: 5px 10px;
      font-size: 14px;
      border: 1px solid #d9dadb;
      border-radius: 20px;
      color: #6f6f70;
    }
  }
}
.history {
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  .btn {
    font-size: 20px;
    padding-right: 2%;
    color: #6f6f70;
  }
}
.listscon {
  margin-top: 10px;
  li {
    display: flex;
    justify-content: space-between;
    align-items: center;
    div {
      font-size: 14px;
      color: black;
      margin-left: 7px;
    }
    p {
      width: 40px;
      height: 20px;
      line-height: 20px;
      text-align: center;
      font-size: 14px;
      color: #6f6f70;
      margin-right: 5px;
    }
  }
}
</style>